package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.SysDeptRoleMapper;
import com.doubi.model.sys.TSysDept;
import com.doubi.model.sys.TSysDeptRole;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 后台部门角色关联表 服务实现类
 * </p>
 *
 * @author MrDoubi
 * @since 2019-11-06
 */
@Service
public class SysDeptRoleService extends ServiceImpl<SysDeptRoleMapper, TSysDeptRole> {

    @Autowired
    private SysDeptRoleMapper sysDeptRoleMapper;

    @Autowired
    private SysDeptService sysDeptService;

    public TSysDeptRole getBeanByDeptOid(String deptOid){
        QueryWrapper<TSysDeptRole> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_oid",deptOid);
        return super.getOne(wrapper);
    }


    /**
     * 功能描述：保存或更新部门信息
     * @author   MrDoubi 2019-06-28 09:55
     * @param sysDept       TSysDept对象
     * @param sysUser       TSysUser对象
     * @return              返回TSysDept对象
     */
    public TSysDept saveOrUpdate(TSysDept sysDept, String roleOid, TSysUser sysUser) {
        if (DataUtil.isEmpty(sysDept.getOid())) {
            TSysDept parent = sysDeptService.getById(sysDept.getParentOid());
            sysDept.setAncestors(parent.getAncestors() + "," + parent.getOid());
            sysDept.setCreateBy(sysUser.getOid());
            sysDept.setCreateTime(new Date());
            sysDept.setUpdateBy(sysUser.getOid());
            sysDept.setUpdateTime(new Date());
            sysDept.setIsDelete(SystemConstants.DELETE_STATUS.STATUS_1);
            sysDept.setStatus(SystemConstants.STATUS.STATUS_1);
            sysDeptService.save(sysDept);

            TSysDeptRole sysDeptRole = new TSysDeptRole();
            sysDeptRole.setRoleOid(roleOid);
            sysDeptRole.setDeptOid(sysDept.getOid());
            sysDeptRoleMapper.insert(sysDeptRole);
            return sysDept;
        } else {
            TSysDept bean = sysDeptService.getById( sysDept.getOid());
            bean.setDeptName(sysDept.getDeptName());
            bean.setSortNum(sysDept.getSortNum());
            bean.setLeader(sysDept.getLeader());
            bean.setPhone(sysDept.getPhone());
            bean.setEmail(sysDept.getEmail());
            bean.setUpdateBy(sysUser.getOid());
            bean.setUpdateTime(new Date());
            sysDeptService.updateById(bean);
            TSysDeptRole sysDeptRole = this.getBeanByDeptOid(sysDept.getOid());
            if (DataUtil.isNotEmpty(sysDeptRole)) {
                sysDeptRole.setRoleOid(roleOid);
                sysDeptRoleMapper.updateById(sysDeptRole);
            } else {
                sysDeptRole = new TSysDeptRole();
                sysDeptRole.setRoleOid(roleOid);
                sysDeptRole.setDeptOid(sysDept.getOid());
                sysDeptRoleMapper.insert(sysDeptRole);
            }
            return bean;
        }
    }

    /**
     * 功能描述：删除部门和下级部门
     * @author   MrDoubi 2019-06-28 09:28
     * @param oid           部门oid
     * @param sysUser       TSysUser对象
     * @return              返回int型结果
     */
    public AppModel deleteBean(String oid, TSysUser sysUser) {
        //获取所有的下级部门
        List<TSysDept> sysDeptList = sysDeptService.getLowDeptListByOid(oid);
        //删除下级部门
        for (TSysDept s : sysDeptList) {
            //删除关联部门
            sysDeptRoleMapper.deleteByMap(new HashMap<String,Object>(){{
                put("dept_oid",s.getOid());
            }});
            s.setIsDelete(SystemConstants.DELETE_STATUS.STATUS_0);
            s.setUpdateBy(sysUser.getOid());
            s.setUpdateTime(new Date());
            sysDeptService.updateById(s);
        }
        //删除自己
        sysDeptService.updateByDel(oid,sysUser.getOid());
        return AppModel.success();
    }

    /**
     * 功能描述：删除部门角色关联
     * @author   MrDoubi 2019-07-02 16:06
     * @param roleOid       角色oid
     */
    public void deleteAllByRoleOid(String roleOid){
        /*sysDeptRoleMapper.deleteByMap(new HashMap<String,Object>(){{
            put("roleOid",roleOid);
        }});*/
        UpdateWrapper<TSysDeptRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_oid",roleOid);
        sysDeptRoleMapper.delete(wrapper);
    }

}
